<?php

define('TBL_PREFIX', 'ps_');

class Shop extends Model {

    var $title   = '';
    var $content = '';
    var $date    = '';
	var $limit	 = 10;

    function Shop()
    {
        // Call the Model constructor
        parent::Model();
    }
	
	/**
	 * lay chi tiet hoa don
	 * @param id int
	 * @param limit int
	 * @param pagenum int
	 */
	function get_bill($id, $limit, $pagenum)
	{
		$this->db->select('bd.*, b.bill_code');
		$this->db->from(TBL_PREFIX. 'bill_detail bd');
		$this->db->join(TBL_PREFIX. 'bill b', 'b.id_bill = bd.id_bill');
		$this->db->where('bd.id_bill', $id);
		//$this->db->order_by('b.date_buy', 'desc');
		$this->db->limit($limit, $pagenum);
		$query = $this->db->get();
		
		// $this->db->join(TBL_PREFIX. 'bill b', 'b.id_supplier = s.id_supplier');
		// $this->db->where('id_bill', $id);
		// $query = $this->db->get(TBL_PREFIX .'bill');
		// $result = $query->result_array();
		// return $result[0];
		return $query;
	}
	
	/**
	 * insert hoa don
	 * @param data array
	 */
	function insert_bill($data)
	{
		$this->db->insert(TBL_PREFIX. 'bill', $data); 
	}
	
	/**
	 * insert chi tiet hoa don
	 * @param data array
	 */
	function insert_bill_detail($data)
	{
		$this->db->insert(TBL_PREFIX. 'bill_detail', $data); 
	}
	
	/**
	 * lay danh sach hoa don mua vao
	 * @param limit int
	 * @param pagenum int
	 */
	function get_bill_list( $limit, $pagenum )
	{
		// normal
		// $this->db->select('*');
		// $this->db->from(TBL_PREFIX. 'bill');
		// $this->db->join(TBL_PREFIX. 'supplier', TBL_PREFIX. 'bill.id_supplier = '. TBL_PREFIX. 'supplier.id_supplier');
		// $this->db->order_by(TBL_PREFIX. 'bill.date_buy', 'desc');
		// $this->db->limit($limit, $pagenum);
		
		// using table alias
		$this->db->select('b.*, s.name as supplier_name');
		$this->db->from(TBL_PREFIX. 'bill b');
		$this->db->join(TBL_PREFIX. 'supplier s', 'b.id_supplier = s.id_supplier');
		$this->db->order_by('b.date_buy', 'desc');
		$this->db->limit($limit, $pagenum);		
		$query = $this->db->get();
		
		// using direct select
		//$query = $this->db->get(TBL_PREFIX .'bill', $limit, $pagenum);
		
		return $query;
		//return $query->result();
		//return $query->result_array();
	}
    
	function get_suppliers()
	{
		$query = $this->db->get(TBL_PREFIX .'supplier');
		return $query;
	}
	
    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }

    function insert_entry()
    {
        $this->title   = $_POST['title']; // please read the below note
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->insert('entries', $this);
    }

    function update_entry()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }

}

function D($a){
	echo "<pre>";
	print_r($a);
	echo "</pre>";
}